package com.woniuxy.carrental.controller;

import com.woniuxy.carrental.entity.Credit;
import com.woniuxy.carrental.entity.PageBean;
import com.woniuxy.carrental.entity.ResponseResult;
import com.woniuxy.carrental.service.CreditService;
import com.woniuxy.carrental.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 贷款申请
 */
@RestController
public class CreditController {

    @Autowired
    CreditService creditService;
    @Autowired
    UserService userService;
    //分页查询客户
    @GetMapping("/selectCredits")
    public ResponseResult<PageBean<Credit>> getCredits(Credit credit, int pageIndex, int pageSize){

        PageBean<Credit> pageBean= new PageBean<>();

        try {
            int i=creditService.selectCreditCount();
            List<Credit> credits = creditService.selectPage(pageIndex, pageSize, credit);
            pageBean.setTotalRecord(i);
            pageBean.setPageIndex(pageIndex);
            pageBean.setPageSize(pageSize);
            pageBean.setBeanList(credits);

            return new ResponseResult<PageBean<Credit>>(pageBean,200,"查询完成");
        } catch (Exception e) {

            return new ResponseResult<PageBean<Credit>>(pageBean,404,"查询失败");
        }

    }

    /*
        客户申请贷款
     */
    @PostMapping("/addCredits")
    public Credit addCredit( Credit credit){


     //   ModelAndView mv=new ModelAndView();
       //设置默认信用值为100
        credit.setPrice(100);
       //贷款状态申请默认为true
        credit.setApply(true);
       //审核默认没通过
       credit.setAudit(false);
       creditService.addCredit(credit);


        return credit;
    }

    /*
    审核通过
     */
    @RequestMapping("/updateCredit/{cid}")
    public  Credit updateCreditByTrue(@PathVariable("cid")Integer cid, Credit credit){

        creditService.updateCreditByTrue(cid);


        return  credit;
    }


}
